<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-row>
        <el-col :span="8" style="text-align: left">
          <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" v-waves
                     :loading="downloadLoading" icon="el-icon-download" @click="exportExcel">导出
          </el-button>
        </el-col>
        <el-col :span="16" style="text-align: right">
          <el-input size="small" style="width: 200px" class="filter-item fsc-search" placeholder="订单号-回车查询"
                    v-model="listQuery.condition.key" @keyup.enter.native="searchClick"></el-input>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-search" @click="searchClick">
            搜索
          </el-button>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-circle-plus-outline"
                     @click="moreClick">更多
          </el-button>
          <el-dropdown trigger="click" :hide-on-click=false style="float: right;margin-left: 10px">
            <span class="el-dropdown-link">
              <el-button class="filter-item" size="small" type="primary" v-waves>
                选择列
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-checkbox-group v-model="theadCheckVal">
                  <el-checkbox label="id">通知单号</el-checkbox>
                  <el-checkbox label="ownerId">客户</el-checkbox>
                  <el-checkbox label="ownerOrderNo">客户订单号</el-checkbox>
                  <el-checkbox label="lotNo1">车架号</el-checkbox>
                  <el-checkbox label="carrierName">供应商</el-checkbox>
                  <el-checkbox label="materielId">车型</el-checkbox>
                  <el-checkbox label="materielName">车型描述</el-checkbox>
                  <el-checkbox label="driverName">司机</el-checkbox>
                  <el-checkbox label="driverPhone">司机联系方式</el-checkbox>
                  <el-checkbox label="status">入库状态</el-checkbox>
                  <el-checkbox label="storeHouseName">收货仓库</el-checkbox>
                  <el-checkbox label="expectDate">预计入库时间</el-checkbox>
                  <el-checkbox label="createDate">入库时间</el-checkbox>
                  <el-checkbox label="remarks">备注</el-checkbox>
                  <el-checkbox label="inspectStatus">质检状态</el-checkbox>
                  <el-checkbox label="gmtCreate">质检时间</el-checkbox>
                  <el-checkbox label="inspectRemark">质检备注</el-checkbox>
                </el-checkbox-group>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
      <div style="clean:both"></div>
    </div>
    <div :class="{'search-transition':searchFlag,h0:true,hAuto:searchFlag,'search-content':true}">
      <el-form ref="form" :model="listQuery.condition" label-width="80px" :inline="true">
        <el-form-item label=单据状态:>
          <el-select v-model="listQuery.condition.status" placeholder="请选择单据状态">
            <el-option label="未入库" value="10"></el-option>
            <el-option label="部分入库" value="20"></el-option>
            <el-option label="全部入库" value="30"></el-option>
            <el-option label="取消入库" value="50"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车架号:">
          <!--<el-input v-model="listQuery.condition.lotNo1" placeholder="请输入车架号"></el-input>-->
          <el-input type="textarea" :rows="2" style="width: 250px"
                    v-model="listQuery.condition.lotNo1"  placeholder="请输入车架号, 以逗号来分隔"></el-input>
        </el-form-item>
        <el-form-item label="客户:">
          <el-input v-model="listQuery.condition.ownerId" placeholder="请输入客户"></el-input>
        </el-form-item>
        <el-form-item label="入库时间:">
          <el-date-picker class="w200" v-model="listQuery.condition.startDate" type="datetime" placeholder="选择日期时间"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          align="right" ></el-date-picker>
          <span class="zhi-font">至</span>
          <el-date-picker class="w200" v-model="listQuery.condition.endDate" type="datetime" placeholder="选择日期时间"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          align="right" ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list" style="margin-left: 10px">查询</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="clean:both"></div>

    <el-table ref="paymentTable" :data="paymentList" v-loading="listLoading" element-loading-text="加载中" border fit
              highlight-current-row style="width: 100%;margin-top:5px">
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column :key="key" align="center" :width="value.width" v-for='(value,key) in tableThead'
                       :label="value.title">
        <template slot-scope="scope">
          <div v-if="value.key === 'status'">{{scope.row[value.key] | inboundLineStatusFilter}}</div>
          <div v-else-if="value.key === 'inspectStatus'">{{scope.row[value.key] | inboundInspectStatusFilter}}</div>
          <div v-else-if="value.key === 'sourceOrderCodeLine'">
            <el-tooltip v-if="scope.row[value.key]" class="item" effect="dark" :content="scope.row[value.key]"
                        placement="top-start">
              <el-button>{{scope.row[value.key] | sourceOrderCodeFilter}}</el-button>
            </el-tooltip>
          </div>
          <div v-else>{{scope.row[value.key]}}</div>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.current" :page-sizes="[10,20,30,50]" :page-size="listQuery.size"
                     layout="total, sizes, prev, pager, next, jumper" :total="listQuery.total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import * as inboundNoticeAPI from '@/api/inbound/inboundNotice'
import waves from '@/directive/waves' // 水波纹指令
import * as Utils from '@/utils/index'
const defautlTableThead = [
  { key: 'id', title: '通知单号', width: 150 },
  { key: 'ownerId', title: '客户', width: '' },
  { key: 'ownerOrderNo', title: '客户订单号', width: 130 },
  { key: 'lotNo1', title: '车架号', width: 180 },
  { key: 'carrierName', title: '供应商', width: 160 },
  { key: 'materielId', title: '车型', width: '' },
  { key: 'materielName', title: '车型描述', width: 120 },
  { key: 'driverName', title: '司机', width: '' },
  { key: 'driverPhone', title: '司机联系方式', width: 130 },
  { key: 'status', title: '入库状态', width: '' },
  { key: 'storeHouseName', title: '收货仓库', width: '' },
  { key: 'expectDate', title: '预计入库时间', width: 150 },
  { key: 'createDate', title: '入库时间', width: 150 },
  { key: 'remarks', title: '备注', width: '' },
  { key: 'inspectStatus', title: '质检状态', width: '' },
  { key: 'gmtCreate', title: '质检时间', width: 150 },
  { key: 'inspectRemark', title: '质检备注', width: '', fixed: 'right' }
]
export default {
  name: 'payment',
  directives: {
    waves
  },
  data() {
    return {
      theadCheckVal: [
        'id',
        'ownerId',
        'ownerOrderNo',
        'lotNo1',
        'carrierName',
        'materielId',
        'materielName',
        'driverName',
        'driverPhone',
        'status',
        'storeHouseName',
        'expectDate',
        'createDate',
        'remarks',
        'inspectStatus',
        'gmtCreate',
        'inspectRemark'
      ],
      tableThead: defautlTableThead,
      listQuery: {
        current: 1,
        size: 10,
        total: 0,
        condition: {
          startDate: Utils.parseTime((new Date()).getTime(), '{y}-{m}-{d} 00:00:00'),
          endDate: Utils.parseTime((new Date()).getTime(), '{y}-{m}-{d} 23:59:59')
        }
      },
      paymentList: [],
      listLoading: true,
      searchFlag: true,
      downloadLoading: false,
      downloadData: []
    }
  },
  created() {
    this.list()
  },
  watch: {
    theadCheckVal(valArr) {
      this.tableThead = defautlTableThead.filter(v => {
        for (let i = 0; i < valArr.length; i++) {
          if (v.key === valArr[i]) {
            return v
          }
        }
      })
    }
  },
  methods: {
    list() {
      // 列表查询
      this.$data.listLoading = true
      // 处理是否展开更多
      let queryParam = {}
      if (this.$data.searchFlag) {
        queryParam = this.$data.listQuery
      } else {
        Object.assign(queryParam, this.$data.listQuery)
        queryParam.condition = {
          startDate: Utils.parseTime((new Date()).getTime(), '{y}-{m}-{d} 00:00:00'),
          endDate: Utils.parseTime((new Date()).getTime(), '{y}-{m}-{d} 23:59:59'),
          key: this.$data.listQuery.condition.key
        }
      }
      queryParam.condition.houseId = this.$store.getters.currentStorehouse.id
      inboundNoticeAPI.selectInboundPage(queryParam).then(result => {
        if (result.code === 0) {
          this.$data.paymentList = result.data.records
          this.$data.listQuery.total = result.data.total
        } else {
          this.$notify({
            title: '信息提示',
            message: result.message,
            type: 'error',
            duration: 2000
          })
        }
        this.$data.listLoading = false
      })
    },
    changeStatus() {
      return this.downloadData.map(i => {
        if (!i.status || i.status === '10') {
          i.status = '未入库'
        } else if (i.status === '20') {
          i.status = '部分入库'
        } else if (i.status === '30') {
          i.status = '全部入库'
        }
        if (!i.inspectStatus || i.inspectStatus === '0') {
          i.inspectStatus = '未质检'
        } else if (i.inspectStatus === '10') {
          i.inspectStatus = '全部合格'
        } else if (i.inspectStatus === '20') {
          i.inspectStatus = '部分合格'
        } else if (i.inspectStatus === '30') {
          i.inspectStatus = '全部破损'
        }
        return i
      })
    },
    exportExcel() {
      // 数据导出
      let queryParam = {}
      if (this.$data.searchFlag) {
        queryParam = this.listQuery.condition
      } else {
        queryParam = {
          orderNo: this.listQuery.condition.key
        }
      }
      queryParam.houseId = this.$store.getters.currentStorehouse.id
      this.downloadLoading = true
      inboundNoticeAPI.exportData(queryParam).then(result => {
        this.downloadLoading = false
        if (result.code === 0) {
          this.downloadData = result.data
          if (this.downloadData.length) {
            const arr = this.changeStatus()
            require.ensure([], () => {
              const { export_json_to_excel } = require('@/vendor/Export2Excel')
              const tHeader = ['客户', '车架号', '车型', '入库状态', '入库时间', '质检状态', '质检时间']
              const filterVal = ['ownerId', 'lotNo1', 'materielId', 'status', 'createDate', 'inspectStatus', 'gmtCreate']
              const data = this.formatJson(filterVal, arr)
              export_json_to_excel(tHeader, data, this.filename)
            })
          }
        } else {
          this.$notify({
            title: '信息提示',
            message: result.message,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleSizeChange(val) {
      // 分页size变化
      this.$data.listQuery.size = val
      this.list()
    },
    handleCurrentChange(val) {
      // 分页当前页变化
      this.$data.listQuery.current = val
      this.list()
    },
    moreClick() {
      // 更多click
      this.$data.searchFlag = !this.$data.searchFlag
    },
    searchClick() {
      // 搜索
      this.list()
    },
    reset() {
      this.$data.listQuery.condition = {
        lotNo1: null,
        status: null,
        key: null
      }
      this.list()
    }
  }
}
</script>
<style scoped>

</style>
